﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllegroIWI
{
	public partial class FormAllegro : Form
	{
		public WebApi webApi;
		public Dictionary<TreeNode, SearchResult> ResultDictionary;
		string selectedUrl;

		public FormAllegro()
		{
			selectedUrl = "";
			InitializeComponent();
			ResultDictionary = new Dictionary<TreeNode, SearchResult>();
			textBoxLogin.Text = "thetrum4n@gmail.com";
			maskedTextBoxPaasword.Text = "";
			textBoxKey.Text = "4544a107";
			textBoxImageAccuracy.Enabled = false;
			comboBoxImage.SelectedIndex = 0;
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			//webApi = new WebApi();
		}


		private void button1_Click(object sender, EventArgs e)
		{
			treeViewResults.Nodes.Clear();
			try
			{
				if (comboBoxImage.SelectedIndex != null)
				{
					int counter = 1;
					webApi.SearchString = textBoxSearch.Text;
					webApi.searchMe();
					var lista_grup = new List<List<SearchResult>>();
					
					var groups = new GroupUtility();
					//groups.doGrouping();
					if (comboBoxImage.SelectedIndex == 0)
					{
						lista_grup = groups.doGrouping(webApi.SearchResults);
					}
					if (comboBoxImage.SelectedIndex == 1)
					{
						lista_grup = groups.doHammingSimiliarComparer(webApi.SearchResults, Int32.Parse(textBoxImageAccuracy.Text));
					}
					if (comboBoxImage.SelectedIndex == 2)
					{
						lista_grup = groups.doChiSimiliarComparer(webApi.SearchResults, Int32.Parse(textBoxImageAccuracy.Text));
					}
					foreach (var group in lista_grup)
					{
						var mainNode = new TreeNode("Image" + counter++);
						foreach (var element in group)
						{
							var node = new TreeNode(element.Name);

							mainNode.Nodes.Add(node);
							ResultDictionary.Add(node, element);
						}

						treeViewResults.Nodes.Add(mainNode);
					}

					richTextBoxOutput.Text += "Search operation successful\n";
				}

			}
			catch (Exception ex)
			{
				richTextBoxOutput.Text += "Error during search operation\n";
				richTextBoxOutput.Text += "Error: " + ex.Message + "\n";
			}
		}

		private void buttonLogin_Click(object sender, EventArgs e)
		{
			try
			{
				webApi = new WebApi(textBoxKey.Text, textBoxLogin.Text, maskedTextBoxPaasword.Text);
				richTextBoxOutput.Text += "Logged in as " + textBoxLogin.Text + ".\n";
			}
			catch (Exception ex)
			{
				richTextBoxOutput.Text += "Could not log in as " + textBoxLogin.Text + ".\n";
				richTextBoxOutput.Text += "Error: " + ex.Message + "\n";
			}

		}

		private void treeViewResults_Click(object sender, EventArgs e)
		{


		}

		private void treeViewResults_AfterSelect(object sender, TreeViewEventArgs e)
		{
			try
			{
				if (ResultDictionary.ContainsKey(treeViewResults.SelectedNode))
				{
					var selectedNode = ResultDictionary[treeViewResults.SelectedNode];
					pictureBoxPicture.Image = selectedNode.Image.Image;
					selectedUrl = selectedNode.Url;
				}
			}
			catch (Exception ex)
			{
				richTextBoxOutput.Text += "Could not find picture.\n";
				richTextBoxOutput.Text += "Error: " + ex.Message + "\n";
			}

		}

		private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsControl(e.KeyChar)
				&& !char.IsDigit(e.KeyChar))
			{
				e.Handled = true;
			}

			// only allow one decimal point
			if (e.KeyChar == '.'
				&& (sender as TextBox).Text.IndexOf('.') > -1)
			{
				e.Handled = true;
			}
		}

		private void textBoxImageAccuracy_TextChanged(object sender, EventArgs e)
		{

		}

		private void comboBoxImage_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (comboBoxImage.SelectedIndex == 1 || comboBoxImage.SelectedIndex == 2)
			{
				textBoxImageAccuracy.Enabled = true;
			}
			else
			{
				textBoxImageAccuracy.Enabled = false;
			}
		}

		private void buttonGroup_Click(object sender, EventArgs e)
		{
			treeViewResults.Nodes.Clear();
			int counter = 1;
			bool success = false;
			var groups = new GroupUtility();
			var group_list = new List<List<SearchResult>>();
			//groups.doGrouping();
			if (comboBoxImage.SelectedIndex == 0)
			{
				group_list = groups.doGrouping(webApi.SearchResults);
				success = true;
			}
			if (comboBoxImage.SelectedIndex == 1 && textBoxImageAccuracy.Text != "")
			{
				group_list = groups.doHammingSimiliarComparer(webApi.SearchResults, Int32.Parse(textBoxImageAccuracy.Text));
				success = true;
			}
			if (comboBoxImage.SelectedIndex == 2 && textBoxImageAccuracy.Text != "")
			{
				group_list = groups.doChiSimiliarComparer(webApi.SearchResults, Int32.Parse(textBoxImageAccuracy.Text));
				success = true;
			}

			if (success)
			{
				foreach (var group in group_list)
				{
					var mainNode = new TreeNode("Image" + counter++);
					foreach (var element in group)
					{
						var node = new TreeNode(element.Name);

						mainNode.Nodes.Add(node);
						ResultDictionary.Add(node, element);
					}

					treeViewResults.Nodes.Add(mainNode);
				}
			}


		}

		private void pictureBoxPicture_Click(object sender, EventArgs e)
		{
			if(selectedUrl != "")
			{
				Process.Start(selectedUrl);
			}
			
		}
	}
}
